package com.jarvis.pzz.modules.my;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jarvis.pzz.R;
import com.jarvis.pzz.common.BaseFragment;
import com.jarvis.pzz.models.LoginModel;
import com.jarvis.pzz.modules.collectionshop.CollectionShopActivity;
import com.jarvis.pzz.modules.login.LoginActivity;
import com.jarvis.pzz.modules.my.view.FeedbackListActivity;
import com.jarvis.pzz.modules.releasedemand.ReleaseDemandActivity;
import com.jarvis.pzz.modules.setup.SetupActivity;
import com.jarvis.pzz.modules.sourceshop.SourceShopActivity;
import com.jarvis.pzz.util.DisplayUtil;
import com.jarvis.pzz.util.SharePreferenceKey;
import com.jarvis.pzz.util.SharePreferenceUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by jarvis.liu on 2016-11-02.
 */

public class MyFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.img_login)
    ImageView img_login;
    @BindView(R.id.rel_title)
    RelativeLayout rel_title;
    @BindView(R.id.rel_person)
    RelativeLayout rel_person;
    @BindView(R.id.ivHead)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.ll_collect)
    LinearLayout ll_collect;
    @BindView(R.id.ll_shop_source)
    LinearLayout ll_shop_source;
    @BindView(R.id.ll_demand)
    LinearLayout ll_demand;
    @BindView(R.id.ll_setting)
    LinearLayout ll_setting;
    @BindView(R.id.ll_feedback)
    LinearLayout ll_feedback;

    private int status = 0;

    public static MyFragment newInstance() {
        Bundle args = new Bundle();
        MyFragment fragment = new MyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_my;
    }

    @Override
    public void initData() {
    }

    @Override
    protected void initView() {
        ViewGroup.LayoutParams vParams = rel_title.getLayoutParams();
        vParams.height = ((DisplayUtil.getMobileWidth(getActivity())) * 16) / 27;
        rel_title.setLayoutParams(vParams);
        setListener();
    }

    /**
     * 设置监听事件
     */
    public void setListener() {
        img_login.setOnClickListener(this);
        ll_collect.setOnClickListener(this);
        ll_shop_source.setOnClickListener(this);
        ll_demand.setOnClickListener(this);
        ll_feedback.setOnClickListener(this);
        ll_setting.setOnClickListener(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setStatusWhite(true);
            System.out.println("un hidden:" + getClass().toString());
            status = 1;
            MobclickAgent.onPageStart(getClass().toString()); //统计页面，"MainScreen"为页面名称，可自定义
            MobclickAgent.onResume(getContext());
        } else {
            System.out.println("hidden:" + getClass().toString());
            status = 2;
            MobclickAgent.onPageEnd(getClass().toString());
            MobclickAgent.onPause(getContext());
        }


    }

    @Override
    public void onPause() {
        super.onPause();
        if (status == 0) {
            System.out.println("onPause:" + getClass().toString());
            MobclickAgent.onPageEnd(getClass().toString());
            MobclickAgent.onPause(getContext());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        setStatusWhite(true);
        if (status == 0) {
            System.out.println("onResume:" + getClass().toString());
            MobclickAgent.onPageStart(getClass().toString()); //统计页面，"MainScreen"为页面名称，可自定义
            MobclickAgent.onResume(getContext());
        }
        if (isLogin()) {
            LoginModel loginModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class);
            rel_person.setVisibility(View.VISIBLE);
            img_login.setVisibility(View.GONE);
            Glide.with(this).load(loginModel.getUserInfo().getUserHeadImg()).crossFade().placeholder(R.drawable.img_default).error(R.drawable.img_default).into(ivHead);
            tv_name.setText(loginModel.getUserInfo().getPhoneNum());

        } else {
            rel_person.setVisibility(View.GONE);
            img_login.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_login:
                startActivity(LoginActivity.class);
                break;
            case R.id.ll_collect:
                //我的收藏
                if (isLogin()) {
                    startActivity(CollectionShopActivity.class);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.ll_shop_source:
                //  我发布的铺源
                if (isLogin()) {
                    startActivity(SourceShopActivity.class);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.ll_demand:
                //我发布的需求
                if (isLogin()) {
                    startActivity(ReleaseDemandActivity.class);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.ll_setting:
                //设置
                startActivity(SetupActivity.class);
                break;
            case R.id.ll_feedback:
                //用户反馈
                startActivity(FeedbackListActivity.class);
                break;

        }
    }
}
